package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    //查询老师方法
    public Teacher[] findAllStudent() {
        return teachers;
    }

    //添加老师方法
    public boolean addTeacher(Teacher teacher, String id) {
        int index = -1;
        //循环遍历找到数组中元素为空的索引
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if (teacher1 == null) {
                //将找到的索引给index，结束循环
                index = i;
                break;
            }
        }

        //判断index值，返回对应布尔值
        if (index == -1) {
            return false;
        } else {
            //index值不为-1，证明数组中有空元素，可以将学生信息添加进数组
            teachers[index] = teacher;
            return true;
        }
    }

    //删除老师方法
    public void deleteTeacherById(String deleteId) {
        int index = getIndex(deleteId);
        //给获取到的索引对象赋值为null
        teachers[index] = null;
    }

    //修改老师方法
    public void updataTeacher(Teacher teacher, String updataId) {
        int index = getIndex(updataId);
        teachers[index] = teacher;
    }

    //获取对象索引
    public int getIndex(String id){
        int index = -1;
        //获取指定对象的索引
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
}
